<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>学生管理</el-breadcrumb-item>
      <el-breadcrumb-item>录入学生</el-breadcrumb-item>
    </el-breadcrumb>
<el-divider></el-divider>
    <el-card style="width: 600px;height: 1130px;margin: 0 auto;border: 1px solid black"><!--卡片-->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="学生姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="学生年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="学生性别:" prop="gender">
        <template>
          <el-radio v-model="ruleForm.gender" label=男></el-radio>
          <el-radio v-model="ruleForm.gender" label=女></el-radio>
        </template>
      </el-form-item>
      <el-form-item label="学生专业:" prop="major">
        <el-select v-model="ruleForm.major" placeholder="请选择专业">
          <el-option label="计算机" value="计算机"></el-option>
          <el-option label="工商管理" value="工商管理"></el-option>
          <el-option label="应用化学" value="应用化学"></el-option>
          <el-option label="地理科学" value="地理科学"></el-option>
          <el-option label="生物科学" value="生物科学"></el-option>
          <el-option label="统计学" value="统计学"></el-option>
          <el-option label="市场营销" value="市场营销"></el-option>
          <el-option label="人力资源管理" value="人力资源管理"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生联系电话" prop="phoneNumber">
        <el-input v-model="ruleForm.phoneNumber"></el-input>
      </el-form-item>
      <el-form-item label="学生身份证" prop="identityCard">
        <el-input v-model="ruleForm.identityCard"></el-input>
      </el-form-item>
      <el-form-item label="学号" prop="getId">
        <el-input v-model="ruleForm.getId"></el-input>
      </el-form-item>
      <el-form-item label="入学时间:" prop="created">
        <el-date-picker
                v-model="ruleForm.created"
                type="date"
                placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
  </div>
</template>


<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        age: "",
        gender: "",
        major: "",
        phoneNumber: "",
        identityCard: "",
        getId: "",
        created: ""
      },
      rules: {
        name: [
          {required: true, message: '请输入学生姓名', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入学生年龄', trigger: 'blur'},
        ],
        phoneNumber: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          { max: 11, message: '长度在 11 个字符', trigger: 'blur'}
        ],
        identityCard: [
          {required: true, message: '请输入身份证号码', trigger: 'blur'},
          { max: 18, message: '长度在 18 个字符', trigger: 'blur'}
          ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9801/students/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
                  .post(url,formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {

  }
}
</script>